package com.test.burnincash.dialog_burnin;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseApplication_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseInfoActivity_burnin;
import com.test.burnincash.constant_burnin.Constant_burnin;
import com.test.burnincash.util_burnin.PermissionUtil_burnin;

import java.io.File;

public class PhotoUploadDialog_burnin extends Dialog {

    private final BaseInfoActivity_burnin activity;

    public static final int Start_Camera = 31, Open_Album = 32;

    public PhotoUploadDialog_burnin(@NonNull BaseInfoActivity_burnin activity_burnin) {
        super(activity_burnin);
        this.activity = activity_burnin;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState_pretfacile) {
        super.onCreate(savedInstanceState_pretfacile);
        setContentView(R.layout.dialog_photo_upload_burnin);
        initView_pretfacile();
    }

    /**
     * 获取沙盒路径
     */
    private String getSandboxFolderPath() {
        String inAppFolder = BaseApplication_burnin.getApplication_burnin().getFilesDir().getAbsolutePath();
        inAppFolder += File.separator;
        inAppFolder += Constant_burnin.PRODUCT;
        inAppFolder += "_PICS";

        File folderFile = new File(inAppFolder);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }

        return inAppFolder;
    }

    /**
     * 获取图片路径
     */
    private String getImageFilePath() {
        String pictureId = System.currentTimeMillis() + "";
        String imgDir = getSandboxFolderPath();
        return (imgDir + File.separator + pictureId + ".jpg");
    }

    @SuppressLint("SetTextI18n")
    private void initView_pretfacile() {

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        findViewById(R.id.dialog_photo_upload_ib_close).setOnClickListener(v -> dismiss());

        findViewById(R.id.dialog_photo_upload_btn_start_camera).setOnClickListener(v -> {
            if (activity == null) return;
            Runnable runnable = () -> {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                //这一步可以改一下：
//                ContentValues values = new ContentValues();
//                activity.photoUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                //
                //
                //改为：
                String imageFilePath = getImageFilePath();
                File imageFile = new File(imageFilePath);
                if (Build.VERSION.SDK_INT < 24) {
                    activity.photoUri_burnin = Uri.fromFile(imageFile);
                } else {
                    String providerAuthority = BaseApplication_burnin.getApplication_burnin().getPackageName() + ".file_provider";
                    activity.photoUri_burnin = FileProvider.getUriForFile(BaseApplication_burnin.getApplication_burnin(), providerAuthority, imageFile);
                }

                intent.putExtra(MediaStore.EXTRA_OUTPUT, activity.photoUri_burnin);
                activity.startActivityForResult(intent, Start_Camera);
                dismiss();
            };
            //申请权限
            if (!PermissionUtil_burnin.checkLaunchPermissions(activity, new String[]{Manifest.permission.CAMERA})) {
                activity.setOnPermissionAllPassRun_burnin(runnable);
                activity.requestLaunchPermissions_burnin(new String[]{Manifest.permission.CAMERA});
            } else runnable.run();
        });

        findViewById(R.id.dialog_photo_upload_btn_open_album).setOnClickListener(v -> {
            if (activity == null) return;
            Runnable runnable = () -> {
                Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                activity.startActivityForResult(intent, Open_Album);
                dismiss();
            };
            runnable.run();
        });
    }

}
